package com.guofen.controller.name;

import com.guofen.pojo.Earthquake;
import com.guofen.pojo.Name;
import com.guofen.service.earthquake.EarthquakeService;
import com.guofen.service.name.NameService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RequestMapping("/earthquake")
@RestController
@Api(tags = "EarthquakeController", description = "UTA assignment")
public class EarthquakeController {

    @Autowired
    private EarthquakeService service;

    @ApiOperation("get all earthquakes")
    @RequestMapping(value = "/getAllEarthquakes",method = RequestMethod.POST)
    public Result getAllName(@RequestParam Integer pageSize,
                             @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getAllEarthquake(pageSize, currentPage));
    }

    @ApiOperation("get earthquakes by mag")
    @RequestMapping(value = "/getByMag",method = RequestMethod.POST)
    public Result getByMag(@RequestParam Double mag1,
                           @RequestParam Double mag2,
                           @RequestParam Integer pageSize,
                           @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByMag(mag1, mag2, pageSize, currentPage));
    }

    @ApiOperation("get magnitude distribution")
    @RequestMapping(value = "/getMagDistribution",method = RequestMethod.POST)
    public Result getMagDistribution(@RequestParam Integer limit) {
        return ResultGenerator.genSuccessResult(service.getMagDistribution(limit));
    }

    @ApiOperation("get depth distribution")
    @RequestMapping(value = "/getDepthDistribution",method = RequestMethod.POST)
    public Result getDepthDistribution(@RequestParam Integer limit) {
        return ResultGenerator.genSuccessResult(service.getDepthDistribution(limit));
    }

    @ApiOperation("get earthquakes by date")
    @RequestMapping(value = "/getByDate",method = RequestMethod.POST)
    public Result getByDate(@RequestParam Long startDate,
                            @RequestParam Long endDate,
                            @RequestParam Integer pageSize,
                            @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByDate(startDate, endDate, pageSize, currentPage));
    }

    @ApiOperation("get earthquakes by time")
    @RequestMapping(value = "/getByTime",method = RequestMethod.POST)
    public Result getByTime(@RequestParam String startTime,
                            @RequestParam String endTime,
                            @RequestParam Integer pageSize,
                            @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByTime(startTime, endTime, pageSize, currentPage));
    }

    @ApiOperation("get earthquakes by distance")
    @RequestMapping(value = "/getByDistance",method = RequestMethod.POST)
    public Result getByDistance(@RequestParam Double lon,
                                @RequestParam Double lat,
                                @RequestParam Double distance,
                                @RequestParam Integer pageSize,
                                @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByDistance(lon, lat, distance,
                                                                      pageSize, currentPage));
    }

    @ApiOperation("get earthquakes by distance and magnitude")
    @RequestMapping(value = "/getByDistanceAndMag",method = RequestMethod.POST)
    public Result getByDistanceAndMag(@RequestParam Double lon,
                                @RequestParam Double lat,
                                @RequestParam Double distance,
                                @RequestParam Double mag1,
                                @RequestParam Double mag2,
                                @RequestParam Integer pageSize,
                                @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByDistanceAndMag(lon, lat, distance,
                mag1, mag2,
                pageSize, currentPage));
    }

    @ApiOperation("get earthquakes by date and mag")
    @RequestMapping(value = "/getByDateAndMag",method = RequestMethod.POST)
    public Result getByDateAndMag(@RequestParam Long startDate,
                                  @RequestParam Long endDate,
                                  @RequestParam Double mag1,
                                  @RequestParam Double mag2,
                                  @RequestParam Integer pageSize,
                                  @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByDateAndMag(startDate, endDate, mag1, mag2,
                                                                        pageSize, currentPage));
    }

    @ApiOperation("get earthquakes by time and mag")
    @RequestMapping(value = "/getByTimeAndMag",method = RequestMethod.POST)
    public Result getByTimeAndMag(@RequestParam String startTime,
                                  @RequestParam String endTime,
                                  @RequestParam Double mag1,
                                  @RequestParam Double mag2,
                                  @RequestParam Integer pageSize,
                                  @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByTimeAndMag(startTime, endTime, mag1, mag2,
                pageSize, currentPage));
    }

    @ApiOperation("get earthquakes by place")
    @RequestMapping(value = "/getByPlace",method = RequestMethod.POST)
    public Result getByPlace(@RequestParam String place,
                             @RequestParam Integer pageSize,
                             @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByPlace(place, pageSize, currentPage));
    }

    @ApiOperation("delete")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") String id) {
        return ResultGenerator.genSuccessResult(service.delete(id));
    }

    @ApiOperation("upload")
    @PostMapping("/upload")
    public Result upload(@RequestParam String time,
                         @RequestParam Double latitude,
                         @RequestParam Double longitude,
                         @RequestParam Double depth,
                         Double mag,
                         String magType,
                         Integer nst,
                         Double gap,
                         Double dmin,
                         @RequestParam Double rms,
                         @RequestParam String net,
                         @RequestParam String id,
                         @RequestParam String updated,
                         @RequestParam String place,
                         @RequestParam String type,
                         Double horizontalError,
                         Double depthError,
                         Double magError,
                         Integer magNst,
                         @RequestParam String status,
                         @RequestParam String locationSource,
                         @RequestParam String magSource){
        Earthquake earthquake = service.getById(id);
        earthquake.setTime(time);
        earthquake.setLatitude(latitude);
        earthquake.setLongitude(longitude);
        earthquake.setDepth(depth);
        earthquake.setMag(mag);
        earthquake.setMagType(magType);
        earthquake.setNst(nst);
        earthquake.setGap(gap);
        earthquake.setDmin(dmin);
        earthquake.setRms(rms);
        earthquake.setNet(net);
        earthquake.setUpdated(updated);
        earthquake.setPlace(place);
        earthquake.setType(type);
        earthquake.setHorizontalError(horizontalError);
        earthquake.setDepthError(depthError);
        earthquake.setMagError(magError);
        earthquake.setMagNst(magNst);
        earthquake.setStatus(status);
        earthquake.setLocationSource(locationSource);
        earthquake.setMagSource(magSource);
        return ResultGenerator.genSuccessResult(service.update(earthquake));
    }
}
